<template>
    <div>
        <home-header></home-header>
        <home-swiper :list='swiperList'></home-swiper>
        <home-icons :list='iconList'></home-icons>
        <home-recommend :list='recommendList'></home-recommend>
        <home-weekend :list='weekendList'></home-weekend>
    </div>
</template>

<script>
    import HomeHeader from './components/Header'
    import HomeSwiper from './components/Swiper'
    import HomeIcons from './components/Icons'
    import HomeRecommend from './components/Recommend'
    import HomeWeekend from './components/Weekend'
    import { mapState } from 'vuex'
    // 获取ajax数据
    import axios from 'axios'
    export default{
        name: 'Home',
        data(){
            return {
                lastCity: '',
                // city: '',
                swiperList: [],
                iconList: [],
                recommendList: [],
                weekendList: []
            }
        },
        components: {
            HomeHeader,
            HomeSwiper,
            HomeIcons,
            HomeRecommend,
            HomeWeekend
        },
        computed: {
            ...mapState(['city'])
        },
        methods: {
            getHomeInfo(){
                axios.get('/api/index.json?city=' + this.city).then(this.getHomeInfoSucc)
            },
            getHomeInfoSucc(res){
                // 打印出数据
                // console.log(res);
                res = res.data
                if(res.ret && res.data){
                    const data = res.data
                    // this.city = data.city
                    this.swiperList = data.swiperList
                    this.iconList = data.iconList
                    this.recommendList = data.recommendList
                    this.weekendList = data.weekendList
                    // console.log(this.city);
                }
            }
        },
        mounted() {
            // 当页面被挂载时
            this.lastCity = this.city
            // console.log('mounted')
            this.getHomeInfo();
        },
        // 当执行keep-alive是，会多出一个activated属性
        // activated怎么样使用的：当页面再一次被使用的时候
        activated() {
            // console.log('activated')
            // 当页面再次被执行的时候
            if(this.lastCity !== this.city){
                this.lastCity = this.city
                // 再次调用该方法
                this.getHomeInfo()
            }
        },
    }
</script>

<style>
    
</style>
